package cn.txzhang.bookstores.modular.business.controller;

import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.txzhang.bookstores.modular.business.service.BookService;
import cn.txzhang.bookstores.modular.business.vo.BookAddDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bookService;

    @PostMapping("/add")
    public ResponseData add(@RequestBody @Validated BookAddDTO data) {
        bookService.saveOrUpdate(data);
        return new ResponseData(true, "", "添加成功！", "");
    }

    @GetMapping("/{id}")
    public ResponseData get(@PathVariable Long id) {
        return new ResponseData(true, "", "", bookService.getById(id));
    }

    @DeleteMapping("/delete")
    public ResponseData delete(Long id) {
        return new ResponseData(true, "", "删除成功！", bookService.removeById(id));
    }

    @GetMapping("/list")
    public ResponseData list(String bookName) {
        return new SuccessResponseData(bookService.list(bookName));
    }
}
